package v1

import (
	"framework-server/model"
	"framework-server/model/request"
	"framework-server/model/response"
	"framework-server/service"
	"fmt"
	"github.com/gin-gonic/gin"
)

func GetSysObjList(c *gin.Context) {
	// 此结构体仅本方法使用
	var info request.GetSysObjListRequest
	_ = c.ShouldBindQuery(&info)
	err, list, total := service.GetSysObjList(info)
	if err != nil {
		response.FailWithMessage(fmt.Sprintf("获取数据失败，%v", err), c)
	} else {
		response.OkWithData(response.PageResult{
			List:     list,
			Total:    total,
			Page:     info.PageInfo.Page,
			PageSize: info.PageInfo.PageSize,
		}, c)
	}
}


func FindSysObj(c *gin.Context) {
	var info model.SysObj
	_ = c.ShouldBindQuery(&info)
	err, resysObj := service.GetSysObj(info.ID)
	if err != nil {
		response.FailWithMessage(fmt.Sprintf("查询失败，%v", err), c)
	} else {
		response.OkWithData(gin.H{"resysObj": resysObj}, c)
	}
}

func CreateSysObj(c *gin.Context) {
	var info model.SysObj
	_ = c.ShouldBindJSON(&info)
	err := service.CreateSysObj(info)
	if err != nil {
		response.FailWithMessage(fmt.Sprintf("创建失败，%v", err), c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

func UpdateSysObj(c *gin.Context) {
	var info model.SysObj
	_ = c.ShouldBindJSON(&info)
	err := service.UpdateSysObj(info)
	if err != nil {
		response.FailWithMessage(fmt.Sprintf("修改数据失败，%v", err), c)
	} else {
		response.OkWithMessage("修改数据成功", c)
	}
}

func DeleteSysObj(c *gin.Context) {
	var info model.SysObj
	_ = c.ShouldBindJSON(&info)
	err := service.DeleteSysObj(info)
	if err != nil {
		response.FailWithMessage(fmt.Sprintf("删除数据失败，%v", err), c)
	} else {
		response.OkWithMessage("删除数据成功", c)
	}
}

func GetAllObjs(c *gin.Context) {
	var info request.GetAllObjsRequest
	_ = c.ShouldBindJSON(&info)
	err, objs := service.GetAllObjs(info)
	if err != nil {
		response.FailWithMessage(fmt.Sprintf("删除数据失败，%v", err), c)
	} else {
		response.OkWithData(map[string]interface{}{
			"objs": objs,
		}, c)
	}
}


func GetObjByAutorityId(c *gin.Context) {
	var info request.GetObjByAutorityIdReqeust
	_ = c.ShouldBindJSON(&info)
	err, objs := service.GetObjByAutorityId(info.AuthorityId)
	if err != nil {
		response.FailWithMessage(fmt.Sprintf("删除数据失败，%v", err), c)
	} else {
		response.OkWithData(map[string]interface{}{
			"objs": objs,
		}, c)
	}
}

func AddObjAuthority(c *gin.Context) {
	var info request.AddObjAuthorityRequest
	_ = c.ShouldBindJSON(&info)
	err := service.AddObjAuthority(info)
	if err != nil {
		response.FailWithMessage(fmt.Sprintf("删除数据失败，%v", err), c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

func GetSysObjMap(c *gin.Context) {
	err, objMap := service.GetSysObjMap()
	if err != nil {
		response.FailWithMessage(fmt.Sprintf("获取数据失败，%v", err), c)
	} else {
		response.OkWithData(map[string]interface{}{
			"objMap": objMap,
		}, c)
	}
}